import indexedDB from "@/plugin/indexedDB/indexedDB.js";
export let db = indexedDB("S-API", 1);
window.db = db;
import * as doc from "./api.doc.js";

export let api = {
  doc
};
export function getObject(object, key = "") {
  if (!object) {
    return;
  }
  let keys = key.split(".");
  let obj = object[keys[0]];
  for (let i = 1; i < keys.length; i++) {
    if (!obj) {
      break;
    }
    obj = obj[keys[i]];
  }
  return obj;
  // return [object, ...keys].reduce((a, b) => {
  // 	return a ? a[b] : undefined;
  // });
}

export function getApi(apiName = "") {
  return getObject(api, apiName);
}

export function getApiMock(apiName = "", param) {
  // console.log("[获取接口模拟数据][getApiMockData][apiName,param,api]", apiName, param, api)
  let opt = getObject(api, apiName);
  return opt && opt.resultMock
    ? opt.resultMock(param)
    : Promise.resolve({
        code: 0,
        message: `接口 [${apiName}] 模拟数据不存在`
      });
}

export default api;
// for (const i in apiDoc) {
//   for (const j in apiDoc[i]) {
//     delete apiDoc[i][j].resultMock;
//   }
// }
